package com.demo.api;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 演示 INetAddress 的使用
 *
 * @author gy
 * @date 2022/5/3
 */
public class API_ {
    public static void main(String[] args) throws Exception {
        // 1、获取本机的InetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        // LAPTOP-MO0PU9FB/192.168.27.1
        System.out.println("localHost = " + localHost);

        // 2、根据名称 获取本机的InetAddress对象
        InetAddress host1 = InetAddress.getByName("LAPTOP-MO0PU9FB");
        System.out.println("host1 = " + host1);

        // 3、根据域名 获取InetAddress对象
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println("host2 = " + host2);

        // 4、通过 InetAddress 对象获取 ip地址
        String hostAddress = host2.getHostAddress();
        System.out.println("host2 对应的ip = " + hostAddress);

        // 5、通过 InetAddress 对象，获取对应的主机名/或者域名
        String hostName = host2.getHostName();
        System.out.println("host2 对应的主机名/或者域名 = " + hostName);

    }
}
